package com.lab.schemeurl.constant;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.cuitrip.app.MainApplication;
import com.cuitrip.business.comment.CommentActivity;
import com.cuitrip.business.home.IndexActivity;
import com.cuitrip.business.home.travel.TravelTripListActivity;
import com.cuitrip.business.login.LoginActivity;
import com.cuitrip.business.order.OrderCreateActivity;
import com.cuitrip.business.order.detail.OrderFormActivity;
import com.cuitrip.business.pay.PayOrderPageActivity;
import com.cuitrip.business.setting.RelationActivity;
import com.cuitrip.business.tripservice.ServiceDetailActivity;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.BaseActivity;
import com.lab.jumper.ActivityManager;
import com.lab.jumper.Jumper;
import com.lab.jumper.JumperManager;
import com.lab.logtrack.AppLog;
import com.lab.schemeurl.SchemeUrlDispatcher;


/**
 * Created by ak
 */
public enum Host {



    RECOMMEND("recommend") {
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            String title = uri.getQueryParameter("title");
            Intent intent = new Intent(context, TravelTripListActivity.class);
            intent.putExtra(BaseActivity.SOURCE_URI_KEY,uri.toString());
            if (!TextUtils.isEmpty(title)){
                intent.putExtra(TravelTripListActivity.TITLE_KEY, title);
            }
            Jumper.launchActivity(context, intent);
//            SimpleEmptyFragmentActivity.startFragmentActivity(context, TravelTripListFragment.class, null);
            return false;
        }
    },
    MY("my") {
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            IndexActivity homeActivity = JumperManager.getInstance().getIndexActivityIfExist();
            if (homeActivity != null) {
                homeActivity.setCurrentTab(IndexActivity.MY_TAB_INDEX);
                return true;
            }
            return false;
        }
    },
    ORDER("order"){
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            try {
                String oid = uri.getQueryParameter("oid");
                if (!TextUtils.isEmpty(oid)){
                    Activity activity = ActivityManager.getInstance().getTopActivity();
                    if (null != activity) {
                        // 当顶层Activity为BrowserWebViewActivity,则退出Activity
                        if (activity instanceof OrderFormActivity) {
                            Jumper.finishActivity(activity);
                            return true;
                        }
                    }
                    Intent intent = OrderFormActivity.getStartOrderIntent(context,oid);
                    context.startActivity(intent);
                }
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
      }
    },
    CHAT("chat"){
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            try {
                String oid = uri.getQueryParameter("oid");
                if (!TextUtils.isEmpty(oid)){
                    Intent notificationIntent = OrderFormActivity.getStartConversationIntent(MainApplication.getInstance(), oid);
                    context.startActivity(notificationIntent);
                }
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
        }
    },
    TRIP("trip"){
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            try {
                String city = uri.getQueryParameter("city");
                String from = uri.getQueryParameter("from");
                String title = uri.getQueryParameter("title");
                String sid = uri.getQueryParameter("sid");
                if (!TextUtils.isEmpty(sid)){
                    ServiceDetailActivity.start(context,sid);

                    return true;
                }
                Intent intent = new Intent(context, TravelTripListActivity.class);
                intent.putExtra(BaseActivity.SOURCE_URI_KEY,uri.toString());
                if (!TextUtils.isEmpty(city)){
                    intent.putExtra(TravelTripListActivity.CITY_KEY, city);
                }else{
                    return false;
                }
                if (!TextUtils.isEmpty(from)){
                    intent.putExtra(TravelTripListActivity.FROM_KEY, from);
                }
                if (!TextUtils.isEmpty(title)){
                    intent.putExtra(TravelTripListActivity.TITLE_KEY, title);
                }
                Jumper.launchActivity(context, intent);
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
        }
    },
    ABOUT("about"){
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            try {
                Intent intent = new Intent(context, RelationActivity.class);
                Jumper.launchActivity(context, intent);
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
        }
    },
    COMMENT("comment"){
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            try {
                String oid = uri.getQueryParameter("oid");
                if (!TextUtils.isEmpty(oid)){
                    Intent intent = new Intent(context, CommentActivity.class);
                    intent.putExtra(CommentActivity.ORDER_ID_KEY, oid);
                    context.startActivity(intent);
                }
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
        }
    },

    CREATE_ORDER("createorder"){
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            try {
                String sid = uri.getQueryParameter("sid");
                if (!TextUtils.isEmpty(sid)){
                    OrderCreateActivity.start(context, sid);
                }
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
        }
    },
    PAY("pay"){
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            try {
                String oid = uri.getQueryParameter("oid");
                if (!TextUtils.isEmpty(oid) && context instanceof Activity){
                    //PayOrderAcivity.startActivity((Activity)context, oid);
                    PayOrderPageActivity.startActivity((Activity) context, oid);
                }
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
        }
    },
    SEARCH("search"){
        @Override
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            if (context instanceof Activity == false) {
                return  false;
            }
            try {
                String searchParam=uri.getQueryParameter("searchParam");
                String language=uri.getQueryParameter("language");
                String title=uri.getQueryParameter("title");
                TravelTripListActivity.search(((Activity)context),title,searchParam,language,uri.toString());
                return true;
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            return false;
        }
    },
    Login("login"){
        @Override
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            TrackUtil.trackScreenName("/account/login");
            Intent intent=new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            return true;
        }
    },
    HOME("home"){
        @Override
        public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
            Intent intent = new Intent(context, IndexActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
            return true;
        }
    };

    private String mHostName;

    Host(String hostName) {
        this.mHostName = hostName;
    }

    public String getHostName() {
        return this.mHostName;
    }

    public static Host getHostByName(String name) {
        if (null != name) {
            for (Host host : Host.values()) {
                if (host.getHostName().equals(name)) {
                    return host;
                }
            }
        }
        return null;
    }

    public boolean perform(Context context, Uri uri, SchemeUrlDispatcher dispatcher) {
        return false;
    }

}
